package jian.hamster.game
{
	import flash.display.Sprite;
	
	import jian.hamster.controller.TimerController;
	import jian.hamster.data.GameData;
	
	/**
	 * 地鼠<br>
	 * @author Jian
	 * 
	 */	
	public class Hamster extends Sprite
	{
		private static const FRAME_STAY:String="STAY";
		private static const FRAME_HURT:String="HURT";
		private static const FRAME_DIE:String="DIE";
		private var _state:int; // 生命状态
		private var _stayCount:int;
		private var _hurtCount:int;
		private var _currentFrame:String;
		
		public function Hamster()
		{
			super();
			this.buttonMode=true;
			this.useHandCursor=true;
			_state = LivingState.DIE;
		}
		
		public function set state($state:int):void
		{
			if(_state == $state)
				return;
			_state = $state;			
		}
		
		private function doHurt():void
		{
			if(_currentFrame != FRAME_HURT)
			{
				GameData.instance.addScore(10 * (1 +  GameData.instance.timePassed / GameData.CONST_TIME_TOTAL));
				this.graphics.clear();
				this.graphics.beginFill(0x00FF00,0.8);
				this.graphics.drawCircle(0,0,20);
				this.graphics.endFill();
				TimerController.instance.deleteTickerCallBack(doDie);
				TimerController.instance.addTickerCallBack(500, doDie, 0);
				_currentFrame = FRAME_HURT;
			}
		}
		
		private function doStay():void
		{
			if(_currentFrame != FRAME_STAY)
			{
				this.graphics.clear();
				this.graphics.beginFill(0xFF0000,0.8);
				this.graphics.drawCircle(0,0,20);
				this.graphics.endFill();
				trace("时间比例" + (GameData.CONST_TIME_TOTAL - GameData.instance.timePassed) / GameData.CONST_TIME_TOTAL);
				TimerController.instance.addTickerCallBack(2500 * ((GameData.CONST_TIME_TOTAL - GameData.instance.timePassed) / GameData.CONST_TIME_TOTAL), doDie, 0);
				_currentFrame = FRAME_STAY;
			}
		}
		
		private function doDie():void
		{
			_stayCount = _hurtCount = 0;
			_state = LivingState.DIE;
			_currentFrame = FRAME_DIE;
			if(this.parent)
			{
				this.parent.removeChild(this);
			}
		}
		
		public function doState():void
		{
			switch(_state)
			{
				case LivingState.STAY:
					doStay();
					break;
				case LivingState.HURT:
					doHurt();
					break;
			}
		}
		
		public function get state():int
		{
			return _state;
		}
	}
}